package com.yf.contentresolver;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void query(View v) {
    	Log.v("ContentResolver", String.valueOf(v.getId()));
    	ContentResolver resolver = getContentResolver();
    	Uri uri = Uri.parse("content://com.yf.provider/query");
		Cursor cursor = resolver.query(uri, null, null, null, null);
		Log.v("ContentResolver", String.valueOf(cursor.getColumnCount()));
		while (cursor.moveToNext()) {
			String name = cursor.getString(cursor.getColumnIndex("name"));
			Log.v("ContentResolver", name);
		}
    }
    
    public void insert(View v) {
    	Log.v("ContentResolver", String.valueOf(v.getId()));
    	ContentResolver resolver = getContentResolver();
    	Uri url = Uri.parse("content://com.yf.provider/insert");
    	ContentValues values = new ContentValues();
    	values.put("name", "dgh");
    	values.put("phone", "13255555555");
		Uri rowId = resolver.insert(url, values);
		Log.v("ContentResolver", rowId.toString());
    }
    
    public void update(View v) {
    	Log.v("ContentResolver", String.valueOf(v.getId()));
    	ContentResolver resolver = getContentResolver();
    	Uri uri = Uri.parse("content://com.yf.provider/update");
    	ContentValues values = new ContentValues();
    	values.put("name", "dingguohua");
		String where = "name=?";
		String[] selectionArgs = {"dgh"};
		int rowCnt = resolver.update(uri, values , where , selectionArgs );
		Log.v("ContentResolver", String.valueOf(rowCnt));
    }
    
    public void delete(View v) {
    	Log.v("ContentResolver", String.valueOf(v.getId()));
    	ContentResolver resolver = getContentResolver();
    	Uri url = Uri.parse("content://com.yf.provider/delete");
    	String where = "name=?";
		String[] selectionArgs = {"lmb"};
		int rowCnt = resolver.delete(url, where, selectionArgs);
		Log.v("ContentResolver", String.valueOf(rowCnt));
    }
}
